﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Authentication
{
	using System.Security.Principal;
	using Falcon.Core.Authorization;
	using Falcon.Core.Extensions;

	public class WebPermissivePrincipal : PermissivePrincipal
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="WebPermissivePrincipal"/> class.
		/// </summary>
		/// <param name="identity">The identity.</param>
		public WebPermissivePrincipal(IIdentity identity) : base(identity)
		{

		}

		/// <summary>
		/// Determines the role permissions.
		/// </summary>
		public override void DetermineRolePermissions()
		{
			var identity = base.Identity as WebUserIdentity;

			if (identity == null || identity.Session == null || !identity.Session.Permissions.HasValue())
			{
				return;
			}

			this.RolePermissions.Clear();

			foreach (var permission in identity.Session.Permissions)
			{
				this.RolePermissions.Add(permission.Role.Title, new IdentityPermission(permission.Action));
			}
		}
	}
}